
import structure from "@/mount/structure";
import { AttackBody, AttackBodyData, BodyNum } from "@/squard/base"
import { generateID, isInArray, unzipPosition, zipPosition } from "@/utils"
import { random } from "lodash";
import { BoostedPartData } from "./boostData"

export default class CreepActionMisson extends Creep {
    // 处理控制冲级任务
    public handle_task_level():void{
        var thisMisson = Game.rooms[this.memory.belong].GainMisson(this.memory.MissonData.MisssonID)
        if (!thisMisson){this.memory.MissonData = {};return}
        var data = thisMisson.data
        if (!data){this.say("找不到任务了");return}
        if (!data.Nolab)
        {
            if (data.boosted && !this.memory.boostData['work'].boosted  && this.ticksToLive > 500)
            {
            /* 去boost */
            var disLab = Game.getObjectById(Object.keys(thisMisson.LabBind)[0])  as StructureLab
            if (disLab) 
            {
                // 计算body部件
                let s = 0
                for (var j of this.body)
                {
                    if (j.type == 'work') s++
                }
                if (!this.pos.isNearTo(disLab)) this.goTo(disLab.pos,1)
                else
                {
                    for (var i of this.body)
                    {
                        if (i.type == 'work' && i.boost != thisMisson.data.rType)
                        {disLab.boostCreep(this);return}
                    }
                    this.memory.boostData['work'] = {boosted:true,num:s,type:thisMisson.data.rType}
                }
            }
            return
            }
        }
        this.workstate('energy')
        if (this.memory.working)
        {
            if (this.upgradeController(this.room.controller) == ERR_NOT_IN_RANGE)
            this.moveTo(this.room.controller.pos)
            this.memory.standed = true
        }
        else
        {
            if (!Game.rooms[this.memory.belong].memory.StructureIdData) return
            var upgrade_link = Game.getObjectById(Game.rooms[this.memory.belong].memory.StructureIdData.upgrade_link)  as StructureLink
            if (!upgrade_link) return
            if(upgrade_link.store.getUsedCapacity('energy') >= this.store.getCapacity())
            {
                if (this.withdraw(upgrade_link,'energy') == ERR_NOT_IN_RANGE)
                {
                    this.goTo(upgrade_link.pos,1)
                }
                return
            }
            else
            {
                var other_link = this.pos.findInRange(FIND_STRUCTURES,2,{filter:(structure)=>{
                    return structure.structureType == 'link' && structure.id != upgrade_link.id && structure.store.getUsedCapacity('energy') > this.store.getCapacity()
                }})
                if (other_link[0])
                {
                    if (this.withdraw(other_link[0],'energy') == ERR_NOT_IN_RANGE)
                    {
                        this.goTo(other_link[0].pos,1)
                    }
                }
            }
        }
    }

    // 处理签名
    public handle_scout():void{
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        if (Game.shard.name != shard || this.room.name != roomName)
        {
            this.say("🥺",true)
            this.arriveTo(new RoomPosition(25,25,roomName),20,shard)
        }
        else
        {
            if (!this.room.controller) return
            if (this.pos.isNearTo(this.room.controller))
            {
                this.signController(this.room.controller,misson.words)
                this.suicide()
            }
            else
            {
                this.goTo(this.room.controller.pos,1)
            }
        }
    }

    // 处理援建一体
    public handle_BoostBuild():void{
        if (!this.memory.MissonData) return
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        if (Game.shard.name != shard || this.room.name != roomName)
        {
            if (this.memory.role == 'architect')
            {
                if (Game.flags[`${this.memory.belong}/debug`])
                {
                    this.goTo(Game.flags[`${this.memory.belong}/debug`].pos,1)
                    return
                }
                /* 没boost先boost */
                for (var body in this.memory.boostData)
                {
                    if (!this.memory.boostData[body].boosted)
                    {
                        console.log('-',body)
                        var tempID:string
                        var thisRoomMisson = Game.rooms[this.memory.belong].GainMisson(this.memory.MissonData.MisssonID)
                        if (!thisRoomMisson) return

                        LoopB:
                        for (var j in thisRoomMisson.LabBind)
                        {
                            if (BoostedPartData[thisRoomMisson.LabBind[j]] && body == BoostedPartData[thisRoomMisson.LabBind[j]])
                            {
                                tempID = j
                                break LoopB
                            }
                        }
                        // console.log('tempID',tempID)
                        if (!tempID) continue
                        // console.log(tempID)
                        var disLab = Game.getObjectById(tempID)  as StructureLab
                        if (!disLab) continue
                        // 计算body部件
                        let s = 0
                        for (var b of this.body)
                        {
                        if (b.type == body) s++
                        }
                        if (!this.pos.isNearTo(disLab)) this.goTo(disLab.pos,1)
                        else
                        {
                            for (var i of this.body)
                            {
                                if (i.type == body && i.boost != thisRoomMisson.LabBind[tempID])
                                {disLab.boostCreep(this);return}
                            }
                            this.memory.boostData[body] = {boosted:true,num:s,type:thisRoomMisson.LabBind[tempID] as ResourceConstant}
                        }
                        return
                    }
                }
                /* boost之后提取能量 */
                if (this.room.name == this.memory.belong && this.memory.shard == Game.shard.name)
                if (this.store.getFreeCapacity() > 0)
                {
                    var storage = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(structure)=>{
                        return structure.structureType == 'storage'
                    }})
                    if (storage) this.withdraw__(storage,'energy')
                    return
                }
                /* 都好了就好上路了 */
                this.arriveTo(new RoomPosition(25,25,roomName),20,shard)
                if (this.hits < this.hitsMax) this.heal(this)
            }
            if (this.memory.role == 'claim')
            {
                this.arriveTo(new RoomPosition(25,25,roomName),20,shard)
            }
        }
        else
        {
            if (this.memory.role == 'architect')
            {
                /* 寻找凋落资源 */
                var drop_energy = this.pos.findClosestByRange(FIND_DROPPED_RESOURCES,{filter:(re)=>{
                    return re.resourceType == 'energy' && re.amount >= 500
                }})
                if (drop_energy && this.store.getFreeCapacity() >= drop_energy.amount)
                {
                    if (!this.pos.isNearTo(drop_energy)) this.goTo(drop_energy.pos,1)
                    else this.pickup(drop_energy)
                    return
                }
                this.workstate('energy')
                if (this.memory.working)
                {
                    var construsition = this.pos.findClosestByRange(FIND_CONSTRUCTION_SITES)
                    if (construsition) this.build__(construsition)
                    else this.upgrade__()
                }
                else
                {
                    var storage = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(structure)=>{
                        return structure.structureType == 'storage' && structure.store.getUsedCapacity('energy') >= this.store.getCapacity()
                    }})
                    if (storage)
                    {
                        this.withdraw__(storage,'energy')
                        return
                    }
                    var source = this.pos.findClosestByRange(FIND_SOURCES_ACTIVE)
                    if (source)
                    {
                        this.harvest__(source)
                    }
                    if (this.ticksToLive < 200 && this.store.getUsedCapacity('energy') > 3500)
                    this.memory.working = true
                }
                
                if (this.hits < this.hitsMax * 0.91)
                this.heal(this)
            }
            if (this.memory.role == 'claim')
            {
                var controller = this.room.controller
                if (controller)
                {
                    if (!this.pos.isNearTo(controller)) this.goTo(controller.pos,1)
                    else this.claimController(controller)
                }
            }
        }
    }

    // 协助升级
    public handle_HelpUpgrade():void{
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        if (Game.shard.name != shard || this.room.name != roomName)
        {

            if (Game.flags[`${this.memory.belong}/debug`])
            {
                this.goTo(Game.flags[`${this.memory.belong}/debug`].pos,1)
                return
            }
            /* 没boost先boost */
            for (var body in this.memory.boostData)
                {
                    if (!this.memory.boostData[body].boosted)
                    {
                        var tempID:string
                        var thisRoomMisson = Game.rooms[this.memory.belong].GainMisson(this.memory.MissonData.MisssonID)
                        if (!thisRoomMisson) return

                        LoopB:
                        for (var j in thisRoomMisson.LabBind)
                        {
                            if (BoostedPartData[thisRoomMisson.LabBind[j]] && body == BoostedPartData[thisRoomMisson.LabBind[j]])
                            {
                                tempID = j
                                break LoopB
                            }
                        }
                        if (!tempID) continue
                        var disLab = Game.getObjectById(tempID)  as StructureLab
                        if (!disLab) continue
                        // 计算body部件
                        let s = 0
                        for (var b of this.body)
                        {
                        if (b.type == body) s++
                        }
                        if (!this.pos.isNearTo(disLab)) this.goTo(disLab.pos,1)
                        else
                        {
                            for (var i of this.body)
                            {
                                if (i.type == body && i.boost != thisRoomMisson.LabBind[tempID])
                                {disLab.boostCreep(this);return}
                            }
                            this.memory.boostData[body] = {boosted:true,num:s,type:thisRoomMisson.LabBind[tempID] as ResourceConstant}
                        }
                        return
                    }
                }
            /* boost之后提取能量 */
            if (this.store.getFreeCapacity() > 0)
            {
                var storage = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(structure)=>{
                        return structure.structureType == 'storage'
                    }})
                if (storage) this.withdraw__(storage,'energy')
                return
            }
                /* 都好了就好上路了 */
                this.arriveTo(new RoomPosition(25,25,roomName),20,shard)
                if (this.hits < this.hitsMax) this.heal(this)

        }
        else
        {
            /* 寻找凋落资源 */
            var drop_energy = this.pos.findClosestByRange(FIND_DROPPED_RESOURCES,{filter:(re)=>{
                return re.resourceType == 'energy' && re.amount >= 300
            }})
            if (drop_energy && this.store.getFreeCapacity() >= drop_energy.amount)
            {
                if (!this.pos.isNearTo(drop_energy)) this.goTo(drop_energy.pos,1)
                else this.pickup(drop_energy)
                return
            }
            if (this.hits < this.hitsMax * 0.9)
            {
                this.heal(this)
                return
            }
            this.workstate('energy')
            if (this.memory.working)
            {
                this.upgrade__()
            }
            else
            {
                var storage = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(structure)=>{
                    return structure.structureType == 'storage' && structure.store.getUsedCapacity('energy') >= 50000
                }})
                if (storage)
                {
                    this.withdraw__(storage,'energy')
                    return
                }
                var source = this.pos.findClosestByRange(FIND_SOURCES_ACTIVE)
                if (source)
                {
                    this.harvest__(source)
                }
                if (this.ticksToLive < 150 && this.store.getUsedCapacity('energy') > 2000)
                    this.memory.working = true
            }
        }
    }

    // 后勤协助
    public handle_HelpRear():void{
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        if (Game.shard.name != shard || this.room.name != roomName)
        {
            if (!this.BoostCheck(['work','carry','move','tough','heal'])) return
            /* boost之后提取能量 */
            if (this.store.getFreeCapacity() > 0)
            {
                var storage = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(structure)=>{
                        return structure.structureType == 'storage'
                    }})
                if (storage) this.withdraw__(storage,misson.rType)
                return
            }
                /* 都好了就好上路了 */
                this.arriveTo(new RoomPosition(25,25,roomName),20,shard)
                if (this.hits < this.hitsMax) this.heal(this)
        }
        else
        {
            if (!this.memory.MissonState) this.memory.MissonState = 1
            if (this.memory.MissonState == 1)
            {
                var storage_ = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(structure)=>{
                    return structure.structureType == 'storage' 
                }})
                if (storage_) this.transfer__(storage_,misson.rType)
                else
                {
                    var construsition = this.pos.findClosestByRange(FIND_MY_CONSTRUCTION_SITES)
                    if (construsition) this.build__(construsition)
                    else
                        this.upgrade__()
                }
                if (this.store.getUsedCapacity(misson.rType) <= 0)
                this.memory.MissonState = 2
            }
            else if (this.memory.MissonState == 2)
            {
                /* 完成主要任务后，进行零碎的后勤任务 */
                // 治疗伤员
                var woundCreep = this.pos.findClosestByRange(FIND_MY_CREEPS,{filter:(creep)=>{
                    return creep.hits < creep.hitsMax
                }})
                if (woundCreep && this.hits >= this.hitsMax)
                {
                    this.moveTo(woundCreep)
                    this.heal(woundCreep)
                    return
                }
                else
                {
                    if (!this.memory.working) this.memory.working = false
                    if (this.memory.working == false && this.store.getUsedCapacity('energy') >= 200)
                        this.memory.working = true
                    if (this.memory.working == true && this.store.getUsedCapacity('energy') == 0)
                        this.memory.working = false
                    if (this.memory.working)
                    {
                        var construsition = this.pos.findClosestByRange(FIND_CONSTRUCTION_SITES)
                        if (construsition) this.build__(construsition)
                        else
                        {
                        if (this.memory.MissonData.id)
                            this.repair__(Game.getObjectById(this.memory.MissonData.id) as StructureRampart)
                        }
                    }
                    else
                    {
                        var ramparts = this.room.getListHitsleast(['rampart','constructedWall'],0)
                        if (ramparts) this.memory.MissonData.id = ramparts.id
                        var ss = this.pos.findClosestByRange(FIND_STRUCTURES,{filter:(structure)=>{
                            return structure.structureType == 'storage' 
                        }}) as StructureStorage
                        if (ss && ss.store.getUsedCapacity('energy') > 200 && this.withdraw(ss,'energy',200) == ERR_NOT_IN_RANGE)
                        {
                            this.goTo(ss.pos,1)
                            return
                        }
                        var source = this.pos.findClosestByRange(FIND_SOURCES_ACTIVE)
                        if (source)
                        {
                            this.harvest__(source)
                        }

                    }
                }
            }
            if (this.hits < this.hitsMax)
            {
                this.heal(this)
            }

        }
    }

    public handle_Aio():void{
        var misson = this.memory.MissonData.data
        var shard = misson.shard
        var roomName = misson.disRoom
        if(this.room.name!= roomName)
        this.heal(this)
        if (Game.shard.name != shard || this.room.name != roomName)
        {
            if (!this.BoostCheck(['ranged_attack','move','tough','heal'])) return
            this.arriveTo(new RoomPosition(24,24,roomName),24,shard)
        }
        else
        {
            /* 寻找黄旗、红旗 */
            var yellowFlag = this.pos.findClosestByPath(FIND_FLAGS,{filter:(flag)=>{
                return flag.name == `${this.memory.belong}/aio`
            }})
            if (yellowFlag && !this.pos.isNearTo(yellowFlag)){this.goTo(yellowFlag.pos,1);return}
            var flag = this.pos.findClosestByPath(FIND_FLAGS,{filter:(flag)=>{
                return flag.color == COLOR_GREEN
            }})
            /* 获取防御塔伤害地形攻击数据 */
            // if (!global.towerRangeData) global.towerRangeData = {}
            // if (!global.towerRangeData[this.room.name]) this.room.TowerRangeMap()
            /* 获取所有有伤害的敌对虫子伤害地形数据 */
            // var HostileCreep = this.pos.findInRange(FIND_HOSTILE_CREEPS,6,{filter:(creep)=>{
            //     return !isInArray(Memory.whitesheets,creep.owner.username) &&
            //     AttackBody(creep)
            // }})
            /* 极限抗压伤害 */
            // var crush = BodyNum(this,HEAL)*12*4
            // if (HostileCreep)
            // {
            //     for (var c of HostileCreep)
            //     {
            //         /* 计算爬虫的伤害 */
            //         var result = AttackBodyData(c)
            //         /* 计算是否会被破防 */
            //         if (global.towerRangeData[this.room.name][`${this.pos.x}/${this.pos.y}`].attack +result['attack'] + result['ranged_attack'] > crush )
            //         {
            //             /* 跑 */
            //             if(Math.abs(this.pos.x-c.pos.x)<=4 && Math.abs(this.pos.y-c.pos.y))
            //             {
            //                 if (yellowFlag) this.goTo(yellowFlag.pos,0)
            //                 else {
            //                     /* 找相反方向 */
            //                     if (this.pos.x < c.pos.x && this.pos.y < c.pos.y) this.move(8)
            //                     else if (this.pos.x < c.pos.x && this.pos.y == c.pos.y) this.move(7)
            //                     else if (this.pos.x < c.pos.x && this.pos.y > c.pos.y) this.move(6)
            //                     else if (this.pos.x == c.pos.x && this.pos.y < c.pos.y) this.move(1)
            //                     else if (this.pos.x == c.pos.x && this.pos.y > c.pos.y) this.move(5)
            //                     else if (this.pos.x > c.pos.x && this.pos.y < c.pos.y) this.move(2)
            //                     else if (this.pos.x > c.pos.x && this.pos.y == c.pos.y) this.move(3)
            //                     else if (this.pos.x > c.pos.x && this.pos.y > c.pos.y) this.move(4)
            //                     return
            //                 }
            //             }
            //         }
            //     }
            // }
            var creep_ = this.pos.findInRange(FIND_HOSTILE_CREEPS,3,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.owner.username) && !creep.pos.GetStructure('rampart')
            }})
            if(creep_.length>0)
            if (this.pos.isNearTo(creep_[0]))
            {
                this.rangedMassAttack()
            }
            else
            {
                this.rangedAttack(creep_[0])
            }
            let otherCreeps = this.pos.findInRange(FIND_MY_CREEPS,3,{filter:(creep)=>{return creep.hits < creep.hitsMax - 200}})
            // this.say(`${otherCreeps.length}`)
            if (otherCreeps[0] && this.hits == this.hitsMax)
            {
                if (otherCreeps[0].pos.isNearTo(this))
                this.heal(otherCreeps[0])
                else {this.rangedHeal(otherCreeps[0]);this.goTo(otherCreeps[0].pos,1);return;}
            }
            else
            {
                this.heal(this)
            }
            /* 攻击建筑 */
            if (!flag)
            {
                this.rangedMassAttack()
                var clostStructure = this.pos.findClosestByPath(FIND_HOSTILE_STRUCTURES,{filter:(struc)=>{
                    return !isInArray([STRUCTURE_CONTROLLER,STRUCTURE_WALL,STRUCTURE_RAMPART,STRUCTURE_STORAGE],struc.structureType) && !struc.pos.GetStructure('rampart')
                }})
                if (clostStructure)
                {
                    clostStructure.pos.createFlag(generateID(),COLOR_GREEN)
                    return
                }
                else
                {return}
            }
            if (!flag) return
            var structures = flag.pos.lookFor(LOOK_STRUCTURES)
            if (structures.length > 0)
            {
            if (this.pos.inRangeTo(structures[0],3)) this.rangedAttack(structures[0])
            if (!this.pos.isNearTo(structures[0]))
                this.moveTo(structures[0])
            else{
                if (isInArray([STRUCTURE_WALL,STRUCTURE_ROAD,STRUCTURE_CONTAINER],structures[0].structureType))
                {
                    this.rangedAttack(structures[0])
                }
                else this.rangedMassAttack()
            }
            }
            else
            {
                this.rangedMassAttack()
                flag.remove()
            }

        }
    }

    // // R型四人小队
    // public handle_XR():void{
    //     var misson = this.memory.MissonData.data
    //     var missonID = this.memory.MissonData.MissonID
    //     /* 小队数据初始化 */
    //     if (!Memory.squardMemory) Memory.squardMemory = {}
    //     if (!Memory.squardMemory[missonID]) 
    //     Memory.squardMemory[missonID] = {
    //         creepData:{
    //             [this.name]:{role:this.memory.role,},
    //         },
    //         array:'free' as arrayType,
    //         sourceRoom:this.memory.belong,
    //         disRoom:misson.disRoom,
    //         disShard:misson.shard,
    //     }
    //     else
    //     {
    //         if (!Memory.squardMemory[missonID].creepData[this.name])
    //             Memory.squardMemory[missonID].creepData[this.name] = {role:this.memory.role}
    //     }
    //     /* 有数据后就框架控制 */

    // }

    // 墙体维护
    public handle_repair():void{
        var misson = this.memory.MissonData.data
        var missonID = this.memory.MissonData.MisssonID
        var globalMisson =  Game.rooms[this.memory.belong].GainMisson(missonID)
        if (!globalMisson) {this.memory.MissonData = {};return}
        var storage_ = global.Stru[this.memory.belong]['storage'] as StructureStorage
        if (!storage_){Game.rooms[this.memory.belong].DeleteMisson(missonID);return}

        if (isInArray([1,2,3],misson.boostType))
        {
            let boo = false
            for (var id in globalMisson.LabBind)
            {
                var lab_ = Game.getObjectById(id) as StructureLab
                if (!lab_ || !lab_.mineralType  ||  lab_.store.getUsedCapacity(lab_.mineralType) < 500)
                boo =true
            }
            if (!boo)
            {
                if (misson.RepairType == 'global')
                {
                    if (!this.BoostCheck(['work'])) return
                }
                else
                {
                if (!this.BoostCheck(['work','carry'])) return
                }
            }
        }
        this.workstate('energy')

        // 防御措施
        if (Game.rooms[this.memory.belong].memory.roomSTATE == 'war')
        {
            var HostileCreep = this.pos.findClosestByRange(FIND_HOSTILE_CREEPS)
            if (HostileCreep && this.pos.inRangeTo(HostileCreep,3))
            {
                var newPosition = new RoomPosition(Game.rooms[this.memory.belong].memory.center_x,Game.rooms[this.memory.belong].memory.center_y,this.memory.belong)
                this.goTo_elude(newPosition,2)
                return
            }
        }
        if (misson.RepairType == 'global')
        {
            if (this.memory.working)
            {
                if (this.memory.targetId)
                {
                    var target_ = Game.getObjectById(this.memory.targetId) as StructureRampart
                    if (!target_) delete this.memory.targetId
                    if (this.repair(target_) == ERR_NOT_IN_RANGE)
                    {
                        this.memory.standed = true
                        this.goTo_elude(target_.pos,3)
                    }
                }
                else
                {
                    this.memory.standed = false
                    var leastRam = this.room.getListHitsleast([STRUCTURE_RAMPART,STRUCTURE_WALL],3)
                    if (!leastRam) return
                    this.memory.targetId = leastRam.id
                }
            }
            else
            {
                /* 寻找hits最小的墙 */
                this.say("×")
                var leastRam = this.room.getListHitsleast([STRUCTURE_RAMPART,STRUCTURE_WALL],3)
                if (!leastRam) return
                this.memory.targetId = leastRam.id
                this.withdraw__(storage_,'energy')
            }
        }
        else if (misson.RepairType == 'nuker')
        {
            /* 防核函数  测试成功！*/
            if (!Game.rooms[this.memory.belong].memory.nukeData) return
            if (Object.keys(Game.rooms[this.memory.belong].memory.nukeData.damage).length <= 0)
            {
                Game.rooms[this.memory.belong].DeleteMisson(missonID)
                return
            }
            /* 优先修spawn和terminal */
            if (!this.memory.targetId)
            {
                for (var dmgPoint in Game.rooms[this.memory.belong].memory.nukeData.damage)
                {
                    if (Game.rooms[this.memory.belong].memory.nukeData.damage[dmgPoint] <= 0) continue
                    var position_ = unzipPosition(dmgPoint)
                    if (!position_.GetStructure('rampart'))
                    {
                        position_.createConstructionSite('rampart')
                        if (!this.memory.working) this.withdraw__(storage_,'energy')
                        else this.build__(position_.lookFor(LOOK_CONSTRUCTION_SITES)[0])
                        return
                    }
                    this.memory.targetId = position_.GetStructure('rampart').id
                    return
                }
                if(!Game.rooms[this.memory.belong].DeleteMisson(missonID)) this.memory.MissonData = {}
                return
            }
            else
            {
                if (!this.memory.working)
                {
                    this.memory.standed = false
                    this.withdraw__(storage_,'energy')
                }
                else
                {
                    this.memory.standed = false
                    if (this.memory.crossLevel > 10) this.memory.crossLevel = 10 - Math.ceil(Math.random()*10)
                    var wall_ = Game.getObjectById(this.memory.targetId) as StructureRampart
                    var strPos = zipPosition(wall_.pos)
                    if (!wall_ || wall_.hits >= Game.rooms[this.memory.belong].memory.nukeData.damage[strPos] + Game.rooms[this.memory.belong].memory.nukeData.rampart[strPos] + 500000)
                    {
                        delete this.memory.targetId
                        Game.rooms[this.memory.belong].memory.nukeData.damage[strPos] = 0
                        Game.rooms[this.memory.belong].memory.nukeData.rampart[strPos] = 0
                        return
                    }
                    if (this.repair(wall_) == ERR_NOT_IN_RANGE)
                    {
                        this.goTo_elude(wall_.pos,3)
                    }
                    
                }
                return
            }
        }
        else if (misson.RepairType == 'special')
        {
            if (this.memory.working)
            {
                if (this.memory.targetId)
                {
                    var target_ = Game.getObjectById(this.memory.targetId) as StructureRampart
                    if (!target_) delete this.memory.targetId
                    if (this.repair(target_) == ERR_NOT_IN_RANGE)
                    {
                        this.goTo_elude(target_.pos,3)
                    }
                }
                else this.memory.working = false
            }
            else
            {
                /* 寻找所有白黄旗，并获取旗下的建筑id */
                var allFlag = this.room.find(FIND_FLAGS,{filter:(flag)=>{
                    return flag.color == COLOR_WHITE &&flag.secondaryColor == COLOR_YELLOW
                }})
                var disStructure:StructureWall | StructureRampart
                for (var i of allFlag)
                {
                    var thisrampart = i.pos.GetStructure('rampart') as StructureRampart
                    var thiswall = i.pos.GetStructure("constructedWall") as StructureWall
                    let tempStructure = thisrampart?thisrampart:thiswall
                    if (!disStructure || disStructure.hits > tempStructure.hits)
                        disStructure = tempStructure
                }
                if (!disStructure) return
                this.memory.targetId = disStructure.id
                this.withdraw__(storage_,'energy')
            }
        }
    }

    // 确认是否boost了,并进行相应Boost
    public BoostCheck(boostBody:string[]):boolean{
        for (var body in this.memory.boostData)
        {
            if (!isInArray(boostBody,body)) continue
            if (!this.memory.boostData[body].boosted)
            {
                var tempID:string
                var thisRoomMisson = Game.rooms[this.memory.belong].GainMisson(this.memory.MissonData.MisssonID)
                if (!thisRoomMisson) return false

                LoopB:
                for (var j in thisRoomMisson.LabBind)
                {
                    if (BoostedPartData[thisRoomMisson.LabBind[j]] && body == BoostedPartData[thisRoomMisson.LabBind[j]])
                    {
                        tempID = j
                        break LoopB
                    }
                }
                if (!tempID) continue
                var disLab = Game.getObjectById(tempID)  as StructureLab
                if (!disLab) continue
                // 计算body部件
                let s = 0
                for (var b of this.body)
                {
                if (b.type == body) s++
                }
                if (!this.pos.isNearTo(disLab)) this.goTo(disLab.pos,1)
                else
                {
                    for (var i of this.body)
                    {
                        if (i.type == body && i.boost != thisRoomMisson.LabBind[tempID])
                        {disLab.boostCreep(this);return false}
                    }
                    this.memory.boostData[body] = {boosted:true,num:s,type:thisRoomMisson.LabBind[tempID] as ResourceConstant}
                }
                return false
            }
        }
        return true
    }

    
}